package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.common.Result;
import com.tan.steamship.domain.RoutesPo;
import com.tan.steamship.domain.ShoppingCartPo;
import com.tan.steamship.mapper.ShoppingCartMapper;
import com.tan.steamship.service.RoutesService;
import com.tan.steamship.service.ShoppingCartService;
import com.tan.steamship.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 85726
* @description 针对表【shopping_cart(购物车表)】的数据库操作Service实现
* @createDate 2025-02-09 13:20:21
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCartPo>
    implements ShoppingCartService{
    @Autowired
    private RoutesService routesService;


    @Override
    public List<CartVo> getCartByUserId(String userId) {
        List<CartVo> cartVoList = new ArrayList<>();
        LambdaQueryWrapper<ShoppingCartPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartPo::getUserId, userId);
        List<ShoppingCartPo> shoppingCartPos = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(shoppingCartPos)) {
            List<Integer> routeIds = shoppingCartPos.stream().map(ShoppingCartPo::getRouteId).collect(Collectors.toList());
            List<RoutesPo> routesPoList = routesService.listByIds(routeIds);
            if (CollUtil.isNotEmpty(routesPoList)) {
                Map<Integer, RoutesPo> routesPoMap = routesPoList.stream().collect(Collectors.toMap(RoutesPo::getId, routesPo -> routesPo));
                for (ShoppingCartPo shoppingCartPo : shoppingCartPos) {
                    setShoppingCarData(cartVoList, routesPoMap, shoppingCartPo);
                }
            }
        }
        return cartVoList;
    }

    private static void setShoppingCarData(List<CartVo> cartVoList, Map<Integer, RoutesPo> routesPoMap, ShoppingCartPo shoppingCartPo) {
        CartVo cartVo = BeanUtil.copyProperties(shoppingCartPo, CartVo.class);
        RoutesPo routesPo = routesPoMap.get(shoppingCartPo.getRouteId());
        cartVo.setArrivalTime(routesPo.getArrivalTime());
        cartVo.setDepartureTime(routesPo.getDepartureTime());
        cartVo.setDestination(routesPo.getDestination());
        cartVo.setDeparture(routesPo.getDeparture());
        cartVo.setDuration(routesPo.getDuration());
        cartVo.setPrice(routesPo.getPrice());
        cartVo.setShipId(routesPo.getShipId());
        cartVo.setShipName(routesPo.getShipName());
        cartVo.setType(routesPo.getType());
        cartVo.setImgUrl(routesPo.getImgUrl());
        cartVo.setViews(routesPo.getViews());
        cartVo.setNum(shoppingCartPo.getNum());
        // TODO 这里默认设为5 后期再动态修改
        cartVo.setMaxNum(5);
        cartVo.setCheck(false);
        cartVo.setRouteId(routesPo.getId());
        cartVoList.add(cartVo);
    }

    @Override
    public Result addCart(Integer routeId, Integer userId) {
        Result result = new Result();
        LambdaQueryWrapper<ShoppingCartPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartPo::getRouteId, routeId);
        queryWrapper.eq(ShoppingCartPo::getUserId, userId);
        queryWrapper.last("limit 1");
        ShoppingCartPo cartPos = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(cartPos)) {
            ShoppingCartPo shoppingCartPo = new ShoppingCartPo();
            shoppingCartPo.setRouteId(routeId);
            shoppingCartPo.setUserId(userId);
            shoppingCartPo.setNum(1);
            this.save(shoppingCartPo);
            result.setCode(200);
            result.setMessage("添加购物车成功");
            RoutesPo routesPo = routesService.getById(routeId);
            CartVo cartVo = BeanUtil.copyProperties(routesPo, CartVo.class);
            cartVo.setId(shoppingCartPo.getId());
            cartVo.setNum(1);
            cartVo.setRouteId(routeId);
            cartVo.setMaxNum(100);
            result.setData(cartVo);
        } else {
//            cartPos.setNum(cartPos.getNum() + 1);
//            this.updateById(cartPos);
            result.setCode(204);
            result.setMessage("该航线已经在购物车");
//            result.setMessage("该航线已经在购物车，数量+1");
        }
        return result;
    }

    @Override
    public void deleteCart(String cartId) {
        this.removeById(cartId);
    }

    @Override
    public void updateCartNum(String cartId, String num) {
        LambdaUpdateWrapper<ShoppingCartPo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShoppingCartPo::getId, cartId);
        updateWrapper.set(ShoppingCartPo::getNum, num);
        this.update(updateWrapper);
    }
}




